package com.persondemo.videoappliction.ui.search;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.HotSearchBean;
import com.persondemo.videoappliction.component.ApplicationComponent;
import com.persondemo.videoappliction.component.DaggerHttpComponent;
import com.persondemo.videoappliction.ui.base.BaseFragment;
import com.persondemo.videoappliction.ui.search.contract.FSContract;
import com.persondemo.videoappliction.ui.search.presenter.FSPresenter;
import com.persondemo.videoappliction.utils.PreferenceUtil;
import com.persondemo.videoappliction.utils.T;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/4/16.
 */

public class SearchFragment extends BaseFragment<FSPresenter> implements FSContract.View {
    @BindView(R.id.fragment_search_sure)
    Button fragmentSearchSure;
    @BindView(R.id.fragment_search_clear)
    Button fragmentSearchClear;
    @BindView(R.id.id_flowlayout)
    TagFlowLayout mFlowLayout;
    List<HotSearchBean.DataBean> list = new ArrayList<>();
    List<String> stringlist = new ArrayList<>();
    @BindView(R.id.framment_search_edit)
    EditText frammentSearchEdit;

    Unbinder unbinder;
    @BindView(R.id.as)
    LinearLayout as;
    private AdView mAdView;

    public static SearchFragment newInstance() {
        Bundle args = new Bundle();
        SearchFragment fragment = new SearchFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.module_fragment_search;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {
        DaggerHttpComponent.builder()
                .applicationComponent(appComponent)
                .build()
                .inject(this);
    }

    //"ca-app-pub-5628217081024947~2270121142"
//        "ca-app-pub-5628217081024947/1092770849"
    @Override
    public void bindView(View view, Bundle savedInstanceState) {
        mFlowLayout.setMaxSelectCount(1);
        String googleid01 = PreferenceUtil.getString("googleid01", "");
        String googleid02 = PreferenceUtil.getString("googleid02", "");
        if (!TextUtils.isEmpty(googleid01) && !TextUtils.isEmpty(googleid02)) {
            //初始化谷歌广告
            MobileAds.initialize(getActivity(), googleid01);
            mAdView = new AdView(getActivity());
            mAdView.setAdSize(AdSize.SMART_BANNER);
            mAdView.setAdUnitId(googleid02);
            AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
            // Optionally populate the ad request builder.
            adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
            as.addView(mAdView);
            // Start loading the ad.
            mAdView.loadAd(adRequestBuilder.build());
        }

    }

    @Override
    public void initData() {
        if (mPresenter != null) {
            mPresenter.getseachdata("", 1);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.fragment_search_sure, R.id.fragment_search_clear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fragment_search_sure:
                if (TextUtils.isEmpty(frammentSearchEdit.getText().toString())) {
                    T.showShort(getActivity(), "搜索内容不能为空！");
                } else {
                    goseachvip(frammentSearchEdit.getText().toString());
                }
                break;
            case R.id.fragment_search_clear:
                frammentSearchEdit.setText("");
                break;
        }
    }

    @Override
    public void loaddata(HotSearchBean vipSeachBean) {
        if (vipSeachBean == null || vipSeachBean.getStatus() != 1) {
            showFaild();
            T.showShort(getActivity(), "加载失败！");
            return;
        }
        if (vipSeachBean.getStatus() == 1 && vipSeachBean.getData().size() != 0) {
            list = vipSeachBean.getData();
            final LayoutInflater mInflater = LayoutInflater.from(getActivity());
            for (int i = 0; i < list.size(); i++) {
                stringlist.add(list.get(i).getName());
            }
            mFlowLayout.setAdapter(new TagAdapter<String>(stringlist) {
                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    TextView tv = (TextView) mInflater.inflate(R.layout.tv,
                            mFlowLayout, false);
                    tv.setText(s);
                    return tv;
                }
            });
            mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    goseachvip(stringlist.get(position).toString());
                    return true;
                }
            });
        }
    }

    private void goseachvip(String title) {
        Intent intent = new Intent(getActivity(), SeachVipActivity.class);
        Bundle b = new Bundle();
        b.putString("title", title);
        intent.putExtras(b);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_in_right);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }
}
